package set;

import java.util.LinkedHashSet;
import java.util.Objects;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 如果name和price一样则不能添加
 * @date 2022/9/25 15:04
 */
@SuppressWarnings({"all"})
public class LinkedHashSetExercise {

    public void test() {
        LinkedHashSet set = new LinkedHashSet();
        set.add(new Car("oyz",12000));
        set.add(new Car("oyz",12000));
        // set.add(new Car("oyz",12000));
        System.out.println(set);

    }

    public static void main(String[] args) {
        new LinkedHashSetExercise().test();
    }

    class Car{
        private String name;

        public int price;


        public Car(String name, int price) {
            this.name = name;
            this.price = price;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Car car = (Car) o;
            return price == car.price && Objects.equals(name, car.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, price);
        }

        @Override
        public String toString() {
            return "\nCar{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    '}';
        }
    }
}
